package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 对于DML(增删改)操作的使用
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2021-12-13-16:46 16:46
 */
public class JdbcDML {
    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql:///cartoondb", "root", "123456");
            String sql=" insert into user(id,username,userpwd,truename,age) values ('9','root01','123123','李四',33)";
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);
            System.out.println("有"+i+"受到影响");
            if (i>0){
                System.out.println("添加成功！");
            }else{
                System.out.println("添加失败！");
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            //进行空指针异常的处理
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
    }
}
